package com.liyunc.demo.detect.camera;

import org.bytedeco.javacv.CanvasFrame;
import org.bytedeco.javacv.Frame;

import javax.swing.JFrame;

/**
 * 摄像头抓拍视频预览.
 */
public class PreviewCamera extends AbstractCameraApplication {

    // 展示视频帧的本地窗口
    protected CanvasFrame previewCanvas;

    /**
     * 初始化操作.
     */
    @Override
    protected void initOutput() {
        previewCanvas = new CanvasFrame("camera preview",
                CanvasFrame.getDefaultGamma() / grabber.getGamma());
        previewCanvas.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        previewCanvas.setAlwaysOnTop(true);
    }

    /**
     * 拿到每一帧视频之后的操作.
     *
     * @param frame 原始帧
     */
    @Override
    protected void output(Frame frame) {
        // 在预览窗口上显示当前帧
        previewCanvas.showImage(frame);
    }

    /**
     * 关闭本地窗口，释放资源.
     */
    @Override
    protected void releaseOutputResource() {
        if (null != previewCanvas) {
            previewCanvas.dispose();
        }
    }
}
